<template>
  <div class="app-container">
    <el-card style="margin-bottom: 10px">
      <el-row :gutter="24" style="margin-bottom: -15px">
        <el-col :span="24" :xs="24">
          <el-form
            :model="queryParams"
            ref="queryForm"
            :inline="true"
            v-show="showSearch"
            label-width="108px"
          >
            <el-form-item label="签约方名称" prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item style="float: right">
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="handleQuery"
                >搜索</el-button
              >
              <el-button icon="el-icon-refresh" @click="resetQuery"
                >重置</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-card>

    <el-card>
      <stylizedTable
        title="签约方管理"
        :selector="false"
        :ordinal="true"
        :queryParams="queryParams"
        :loading="false"
        :total="total"
        :tableItem="tableItem"
        :tableData="ContractPartyList"
        :buttonList="buttonList"
        @getList="getList"
        @reset="getList"
      />
    </el-card>

    <!-- 添加或修改签约方对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="签约方名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入签约方名称" />
        </el-form-item>
        <el-form-item label="联系电话" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="负责人" prop="director">
          <el-input v-model="form.director" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="纳税人编号" prop="taxpayerNumber">
          <el-input
            v-model="form.taxpayerNumber"
            placeholder="请输入纳税人编号"
          />
        </el-form-item>
        <el-form-item label="开户名称" prop="accountName">
          <el-input v-model="form.accountName" placeholder="请输入开户名称" />
        </el-form-item>
        <el-form-item label="银行账户" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入银行账户" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listContractParty,
  getContractParty,
  delContractParty,
  addContractParty,
  updateContractParty,
} from "@/api/contract/ContractParty";

export default {
  name: "ContractParty",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 签约方表格数据
      ContractPartyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      buttonList: [
        {
          icon: "el-icon-circle-plus-outline",
          type: "primary",
          text: "添加",
          methods: this.addContractPartyButton,
        },
      ],
      tableItem: [
        {
          porp: "签约方名称",
          data: "name",
          fixed: "left",
          width: 200,
          solt: { name: 1 },
        },
        {
          porp: "联系电话",
          data: "telephone",
          width: 200,
          solt: { name: 1 },
        },
        {
          porp: "地址",
          data: "address",
          width: 230,
          solt: { name: 1 },
        },
        {
          porp: "负责人",
          data: "director",
          solt: { name: 1 },
        },
        {
          porp: "纳税人识别号",
          data: "taxpayerNumber",
          width: 230,
          solt: { name: 1 },
        },
        {
          porp: "开户名称",
          data: "accountName",
          width: 230,
          solt: { name: 1 },
        },
        {
          porp: "银行账号",
          data: "bankAccount",
          width: 230,
          solt: { name: 1 },
        },
        {
          porp: "备注",
          data: "remark",
          solt: { name: 1 },
        },
        {
          porp: "操作",
          fixed: "right",
          width: 100,
          solt: {
            name: 5,
            display: 0,
            operate: [
              {
                text: "修改",
                icon: "editTable",
                methods: this.putContractPartyButton,
                mark: "修改",
                markIcon: "editTable02",
              },
              {
                text: "停用",
                icon: "stop",
                methods: this.handleStatusChange,
                mark: "启用",
                markIcon: "stop02",
                methods02: this.handleStatusChange,
              },
            ],
          },
        },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        telephone: null,
        address: null,
        director: null,
        taxpayerNumber: null,
        accountName: null,
        bankAccount: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "签约方名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询签约方列表 */
    getList() {
      this.loading = true;
      listContractParty(this.queryParams).then((response) => {
        this.ContractPartyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    addContractPartyButton() {
      console.log("新建");
      this.$router.push({ path: "/system/ContractParty/add" });
    },
    putContractPartyButton(row) {
      console.log("修改");
      this.$router.push({
        path: "/system/ContractParty/put",
        query: {
          contractPartyId: row.contractPartyId,
        },
      });
    },
    //启用/禁用方法
    handleStatusChange(row) {
      console.log(row);
      // 保存原始状态
      let originalStatus = row.state;
      let actionText = row.state === "0" ? "停用" : "启用";

      this.$confirm(`确认要${actionText}'${row.name}'吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning",
      })
        .then(() => {
          // 根据状态设置新的值
          let newStatus = row.state === "1" ? "0" : "1";
          console.log(row.contractPartyId);
          console.log(newStatus);
          let body = {
            contractPartyId: row.contractPartyId,
            state: newStatus,
          };
          updateContractParty(body).then(() => {
            this.$modal.msgSuccess("操作成功");
            this.getList(); // 成功后刷新列表
          });
        })
        .catch(() => {
          // 取消时恢复原来的状态
          row.status = originalStatus;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        contractPartyId: null,
        name: null,
        telephone: null,
        address: null,
        director: null,
        taxpayerNumber: null,
        accountName: null,
        bankAccount: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.contractPartyId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加签约方";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const contractPartyId = row.contractPartyId || this.ids;
      getContractParty(contractPartyId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改签约方";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.contractPartyId != null) {
            updateContractParty(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContractParty(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const contractPartyIds = row.contractPartyId || this.ids;
      this.$modal
        .confirm('是否确认删除签约方编号为"' + contractPartyIds + '"的数据项？')
        .then(function () {
          return delContractParty(contractPartyIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "contract/ContractParty/export",
        {
          ...this.queryParams,
        },
        `ContractParty_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
